package com.apesay.redis.client;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class RedisClient {
    private Socket socket;
    private OutputStream outputStream;
    private InputStream inputStream;
    String host = "127.0.0.1";
    int port = 6379;

    public RedisClient(){
        init();
    }
    public RedisClient(String host, int port){
        this.host = host;
        this.port = port;
        init();
    }

    private void init(){
        try {
            this.socket = new Socket(host,port);
            this.outputStream = this.socket.getOutputStream();
            this.inputStream = this.socket.getInputStream();
        } catch (IOException e) {
            // TODO 处理异常情况
            e.printStackTrace();
        }
    }

    public String set(final String key, String value) {
        StringBuilder sb = new StringBuilder();
        //虽然输出的时候，会被转义，然而我们传送的时候还是要带上\r\n
        sb.append(Constants.OPERATE_PRE_ARR).append("3").append(Constants.OPERATE_CRLF);
        sb.append(Constants.OPERATE_PRE_BSTR).append(Constants.OPERATE_SET.length()).append(Constants.OPERATE_CRLF);
        sb.append(Constants.OPERATE_SET).append(Constants.OPERATE_CRLF);
        sb.append(Constants.OPERATE_PRE_BSTR).append(key.length()).append(Constants.OPERATE_CRLF);
        sb.append(key).append(Constants.OPERATE_CRLF);
        sb.append(Constants.OPERATE_PRE_BSTR).append(value.length()).append(Constants.OPERATE_CRLF);
        sb.append(value).append(Constants.OPERATE_CRLF);
        byte[] bytes= new byte[1024];
        try {
            outputStream.write(sb.toString().getBytes());
            inputStream.read(bytes);
        } catch (IOException e) {
            // TODO 
            e.printStackTrace();
        }
        return new String(bytes);
    }

    public String get(final String key){
        StringBuilder sb = new StringBuilder();
        //虽然输出的时候，会被转义，然而我们传送的时候还是要带上\r\n
        sb.append(Constants.OPERATE_PRE_ARR).append("2").append(Constants.OPERATE_CRLF);
        sb.append(Constants.OPERATE_PRE_BSTR).append(Constants.OPERATE_GET.length()).append(Constants.OPERATE_CRLF);
        sb.append(Constants.OPERATE_GET).append(Constants.OPERATE_CRLF);
        sb.append(Constants.OPERATE_PRE_BSTR).append(key.length()).append(Constants.OPERATE_CRLF);
        sb.append(key).append(Constants.OPERATE_CRLF);
        byte[] bytes= new byte[1024];
        try {
            outputStream.write(sb.toString().getBytes());
            inputStream.read(bytes);
            String result = new String(bytes);
            if (result.startsWith(Constants.OPERATE_PRE_BSTR)){
                int index = result.indexOf(Constants.OPERATE_CRLF);
                result = result.substring(index+1);
                return result;
            }
        } catch (IOException e) {
            // TODO
            e.printStackTrace();
        }
        return null;
    }

    public void destroy(){
        if (outputStream!=null){
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (inputStream!=null){
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (socket!=null){
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
